package com.icedevcloud.upms.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.icedevcloud.common.satoken.service.SaPermission;
import com.icedevcloud.common.security.util.SecurityUtil;
import com.icedevcloud.upms.entity.SysRole;
import com.icedevcloud.upms.service.ISysPermissionService;
import com.icedevcloud.upms.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 自定义权限加载 服务实现类
 * </p>
 *
 * @author dengbing
 * @since 2023-08-24
 */
@RequiredArgsConstructor
@Component
public class SaPermissionImpl extends SaPermission {

    private final ISysPermissionService iSysPermissionService;
    private final ISysRoleService iSysRoleService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Long userId = Long.valueOf(loginId.toString());
        if (SecurityUtil.isSuperAdminRole()) {
            return ListUtil.of("*:*:*");
        }
        return iSysPermissionService.getUserPermCodeByUserId(userId);
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Long userId = Long.valueOf(loginId.toString());
        SysRole sysRole = iSysRoleService.getRoleByUserId(userId);
        if (sysRole != null) {
            return ListUtil.of(sysRole.getRoleCode());
        } else {
            return Collections.emptyList();
        }
    }

}
